//
//  PlacesViewController.m
//  Places
//
//  Created by alex on 9/21/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "PlacesViewController.h"
#import "ViewControllerData.h"
#import "DataManagementSingleton.h"
#import <CoreLocation/CoreLocation.h>

@implementation PlacesViewController

#pragma mark 
#pragma mark properties

@synthesize mapView, toolbar, placeButton;
@synthesize touchView;
@synthesize annotationImage, annotationButton;

- (TouchView *)touchView {
	if (touchView == nil) {
		touchView = [[TouchView alloc] initWithDelegate:self];
	}
	
	return touchView;
}

- (UIImage *)annotationImage {
	if (annotationImage == nil) {
		annotationImage = [UIImage imageNamed:@"mapPin.png"];
	}
	
	return annotationImage;
}

- (UIButton *)annotationButton {
	if (annotationButton == nil) {
		annotationButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
	}
	
	return annotationButton;
}

#pragma mark 
#pragma mark view routine (fetching and updating map data)

- (void)viewDidLoad {
	[super viewDidLoad];
	
	// creating place deccription controller
	placeDescriptionController = [[PlaceDescriptionController alloc] initWithNibName:@"PlaceDescriptionController" bundle:nil];
	placeDescriptionController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	
	// fetching data
	NSEntityDescription *entity = [NSEntityDescription entityForName:@"PlaceData" inManagedObjectContext:[UIViewController managedObjectContext]];
	NSFetchRequest *request = [[NSFetchRequest alloc] init];
	[request setEntity:entity];
	
	NSError *error;
	NSArray *fetchResults = [[UIViewController managedObjectContext] executeFetchRequest:request error:&error];
	if (fetchResults == nil) {
		NSLog(@"Fetching data error - %@", [error localizedDescription]);
	}
	
	for (PlaceData *place in fetchResults) {
		PlaceAnnotation *annotation = [[PlaceAnnotation alloc] initWithPlaceData:place];
		[mapView addAnnotation:annotation];
		[annotation release];
	}
	
	[request release];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	// updating annotation callout
	NSArray *selectedAnnotations = mapView.selectedAnnotations;
	
	for (PlaceAnnotation *annotation in selectedAnnotations) {
		[mapView deselectAnnotation:annotation animated:NO];
		[mapView selectAnnotation:annotation animated:NO];
	}
}

#pragma mark 
#pragma mark Place mark on the map

- (IBAction)placeMark {
	if (!self.touchView.isShowing) {
		placeButton.style = UIBarButtonItemStyleDone;
		[mapView addSubview:self.touchView];
	}
	else {
		placeButton.style = UIBarButtonItemStyleBordered;
		[self.touchView hide];
	}
}

#pragma mark
#pragma mark dealloc

- (void)dealloc {
	[mapView release];
	[toolbar release];
	[placeButton release];
	[touchView release];
	[annotationImage release];
	[annotationButton release];
	[placeDescriptionController release];
	[super dealloc];
}

#pragma mark 
#pragma mark TouchView delegate's routine

- (void)addAnnotationAt:(CGPoint)point {
	CLLocationCoordinate2D location = [mapView convertPoint:point toCoordinateFromView:touchView];
	
	PlaceData *newPlace = (PlaceData *)[NSEntityDescription insertNewObjectForEntityForName:@"PlaceData" inManagedObjectContext:[UIViewController managedObjectContext]];
	newPlace.latitude = [NSNumber numberWithDouble:location.latitude];
	newPlace.longitude = [NSNumber numberWithDouble:location.longitude];

	PlaceAnnotation *addAnnotation = [[PlaceAnnotation alloc] initWithPlaceData:newPlace];
	[mapView addAnnotation:addAnnotation];
	
	[addAnnotation release];
	 
	NSError *error;
	if (![[UIViewController managedObjectContext] save:&error]) {
		NSLog(@"Save error - %@", [error localizedDescription]);
	}
}

#pragma mark 
#pragma mark mapView annotation's routine

- (MKAnnotationView *) mapView:(MKMapView *) localMapView viewForAnnotation:(id) annotation {
	static NSString *annotationIdentifier = @"customAnnotation";
	MKAnnotationView *customAnnotation = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];
	
	if (customAnnotation == nil) {
		customAnnotation = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
		customAnnotation.image = self.annotationImage;
		customAnnotation.centerOffset = CGPointMake(9, -20);
		customAnnotation.canShowCallout = YES;
		customAnnotation.rightCalloutAccessoryView = self.annotationButton;
	}
	else {
		customAnnotation.annotation = annotation;
	}

    return customAnnotation;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
	PlaceAnnotation *placeAnnotation = (PlaceAnnotation *)view.annotation;
	placeDescriptionController.annotation = placeAnnotation;
	[placeDescriptionController editAnnotation];
	[self presentModalViewController:placeDescriptionController animated:YES];
}

#pragma mark 
#pragma mark removing annotation from map

- (void)removeAnnotation:(id <MKAnnotation>)annotation {
	[mapView removeAnnotation:annotation];
}

@end
